work on aftership agent

Judy Ngai 9 years ago
parent
commit
e0dad10deb
1 changed files with 63 additions and 0 deletions
  1. 63 0
      app/models/agents/aftership_agent.rb

+ 63 - 0
app/models/agents/aftership_agent.rb

@@ -0,0 +1,63 @@
1
+module Agents
2
+  class AftershipAgent < Agent
3
+
4
+    API_URL = 'https://api.aftership.com/v4/couriers/all'
5
+    HEADERS = {"aftership-api-key"=>"api_key", "Content-Type"=>"application/json"}
6
+
7
+    description <<-MD
8
+
9
+      The Aftership agent allows you to track your shipment data from aftership.
10
+
11
+      To be able to use the Aftership API, you need to generate an `API Key`.
12
+      You can generate an api key by visiting `apps > app and click add` on aftership website. 
13
+
14
+      The agent is limited to 600 reqs/min per account. You do need a paying plan to use their tracking feature.
15
+
16
+      Required Options:
17
+
18
+      * `Content-Type` application/json
19
+      * `aftership_api_key` - YOUR_API_KEY.
20
+    MD
21
+
22
+    event_description <<-MD
23
+      Events look like this:
24
+
25
+    {
26
+      "meta": {
27
+        "code": 200
28
+      },
29
+      "data": {
30
+        "couriers": [
31
+           { ... }, 
32
+           { ... },
33
+           { ... }
34
+           ] 
35
+       }
36
+     }
37
+      MD
38
+
39
+    def default_options
40
+      { 'aftership_api_key' => 'YOUR_API_KEY',
41
+        'Content_Type' => 'application/json'
42
+      }
43
+    end
44
+
45
+    def working?
46
+      !recent_error_logs?
47
+    end
48
+
49
+    def validate_options
50
+      errors.add(:base, "You need to specify a aftership api key") unless options['aftership-api-key'].present?
51
+      #errors.add(:base, "Content-Type must be set to application/json") unless options['aftership-api-key'].present? && options['aftership-api-key'] == 'application/json'
52
+    end
53
+
54
+    def aftership
55
+      HTTParty.get("https://api.aftership.com/v4/couriers/all", :headers => HEADERS)
56
+    end
57
+
58
+    def check
59
+      data = {"body" => aftership.body, "message" => aftership.message}
60
+      create_event :payload => data
61
+    end
62
+  end
63
+end